package com.md.youyouhui.ui.shopping;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.ActivityShoppingBinding;
import com.md.youyouhui.databinding.AdapterShoppingTopBinding;
import com.md.youyouhui.net.response.GetSellResponse;
import com.md.youyouhui.net.response.GetSlideResponse;
import com.md.youyouhui.net.response.GetTypeResponse;
import com.md.youyouhui.net.response.NoticeResponse;
import com.md.youyouhui.ui.BaseFragment;
import com.md.youyouhui.ui.adapter.DataBoundViewHolder;
import com.md.youyouhui.ui.adapter.ShoppingTitleAdapter;
import com.md.youyouhui.utils.GlideApp;
import com.md.youyouhui.utils.GlideImageLoader;
import com.md.youyouhui.utils.ToastUtils;
import com.md.youyouhui.view.MainViewModel;
import com.sunfusheng.marqueeview.MarqueeView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by AdministratorZJ
 * on 2019/5/21.
 */
public class ShoppingActivity extends BaseFragment {
    private ActivityShoppingBinding shoppingBinding;
    private MyTitleAdapter titleAdapter;
    private ShoppingTitleAdapter adapter;
    private MainViewModel viewModel;
    private List<String> images = new ArrayList<>();
    private List<String> links = new ArrayList<>();
    private List<String> stringList = new ArrayList<>();
    private int widths;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        shoppingBinding = DataBindingUtil.inflate(inflater, R.layout.activity_shopping, container, false);
        viewModel = new MainViewModel();
        initView();
        initData();
        return shoppingBinding.getRoot();
    }

    public void initView() {
    }

    public void initData() {
        widths = mActivity.getWindowManager().getDefaultDisplay().getWidth();
        initViewPager();
        /*shoppingBinding.marqueeView.startWithText("车内装饰免费送!汽车用品全部五折！");
        shoppingBinding.marqueeView.startFlipping();*/
        titleAdapter = new MyTitleAdapter();
        shoppingBinding.topRecycler.setAdapter(titleAdapter);
        adapter = new ShoppingTitleAdapter(mActivity);
        shoppingBinding.shoppingRecycler.setAdapter(adapter);
        getNotice();
        getType();
        getSellShopping();
    }

    private void getSellShopping() {
        viewModel.getShoppingSell().observe(this, listResource -> {
            assert listResource != null;
            switch (listResource.status) {
                case LOADING:
                    shoppingBinding.progress.setVisibility(View.VISIBLE);
                    break;
                case SUCCESS:
                    shoppingBinding.progress.setVisibility(View.GONE);
                    List<GetSellResponse> data = listResource.data;
                    adapter.upDate(data);
                    break;
                case ERROR:
                    shoppingBinding.progress.setVisibility(View.GONE);
                    ToastUtils.errMake(mActivity, listResource.errorCode);
                    break;
                default:
                    break;
            }
        });
    }

    private void getType() {
        viewModel.getShoppingType().observe(this, listResource -> {
            assert listResource != null;
            switch (listResource.status) {
                case LOADING:
                    break;
                case SUCCESS:
                    List<GetTypeResponse> data = listResource.data;
                    titleAdapter.upDate(data);
                    break;
                case ERROR:
                    ToastUtils.errMake(mActivity, listResource.errorCode);
                    break;
                default:
                    break;
            }
        });
    }

    private void initViewPager() {
        ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) shoppingBinding.banner.getLayoutParams();
        layoutParams.width = widths;
        layoutParams.height = (int) (widths / 1.8);
        shoppingBinding.banner.setLayoutParams(layoutParams);
        viewModel.getSlide().observe(this, listResource -> {
            assert listResource != null;
            switch (listResource.status) {
                case LOADING:
                    break;
                case SUCCESS:
                    List<GetSlideResponse> data = listResource.data;
                    if (null == data) {
                        return;
                    }
                    for (int i = 0; i < data.size(); i++) {
                        images.add(data.get(i).cover);
                        links.add(data.get(i).link);
                    }
                    showViewpage();
                    break;
                case ERROR:
                    ToastUtils.errMake(mActivity, listResource.errorCode);
                    break;
                default:
                    break;
            }
        });
    }

    private void showViewpage() {
        /*homeBinding.banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        homeBinding.banner.setIndicatorGravity(BannerConfig.CENTER);*/
        shoppingBinding.banner.setDelayTime(3000);
        //设置图片源
        shoppingBinding.banner.setImages(images);
        shoppingBinding.banner.setImageLoader(new GlideImageLoader());
        //设置标题源
        //homeBinding.banner.setBannerTitles(titles);
        shoppingBinding.banner.start();
    }

    private void getNotice() {
        viewModel.notice().observe(this, listResource -> {
            assert listResource != null;
            switch (listResource.status) {
                case SUCCESS:
                    List<NoticeResponse> data = listResource.data;
                    if (null == data) {
                        return;
                    }
                    for (int i = 0; i < data.size(); i++) {
                        stringList.add(data.get(i).content);
                    }
                    showNotice();
                    break;
                case ERROR:
                    ToastUtils.errMake(mActivity, listResource.errorCode);
                    break;
                default:
                    break;
            }
        });
    }

    private void showNotice() {
        shoppingBinding.marqueeView.startWithList(stringList);
        shoppingBinding.marqueeView.startFlipping();
        shoppingBinding.marqueeView.setOnItemClickListener(new MarqueeView.OnItemClickListener() {
            @Override
            public void onItemClick(int position, TextView textView) {
                Intent intent = new Intent(mActivity, NoticeActivity.class);
                Log.e("noticeData",stringList.get(position));
                intent.putExtra("content", stringList.get(position));
                startActivity(intent);

            }
        });
    }


    class MyTitleAdapter extends RecyclerView.Adapter<DataBoundViewHolder<AdapterShoppingTopBinding>> {
        List<GetTypeResponse> data;

        void upDate(List<GetTypeResponse> data) {
            this.data = data;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public DataBoundViewHolder<AdapterShoppingTopBinding> onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            AdapterShoppingTopBinding binding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.adapter_shopping_top, viewGroup, false);
            return new DataBoundViewHolder<>(binding);
        }

        @Override
        public void onBindViewHolder(@NonNull DataBoundViewHolder<AdapterShoppingTopBinding> holder, int i) {
            holder.binding.titles.setText(data.get(i).title);
            GlideApp.with(ShoppingActivity.this).load(data.get(i).icon).centerCrop().into(holder.binding.iv);
            holder.itemView.setOnClickListener(v -> startActivity(new Intent(mActivity, ShoppingMoreActivity.class)
                    .putExtra("title", data.get(i).title).putExtra("id", data.get(i).id)));
        }

        @Override
        public int getItemCount() {
            return null == data ? 0 : data.size();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        shoppingBinding.marqueeView.stopFlipping();
        shoppingBinding.banner.stopAutoPlay();
    }
}
